using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using MacomberMap.Common.Logging;

namespace MacomberMap.Common.Internals
{
    /// <summary>
    /// This class holds the permissions restrictions on the Macomber Map.
    /// </summary>
    public static class MM_Permissions
    {
        #region Variable declaration
        /// <summary>Whether ownership information should be shown</summary>
        public static bool ShowOwnership = true;

        /// <summary>Whether DUNS information should be shown</summary>
        public static bool ShowDUNS = true;

        /// <summary>Whether the user can switch different models</summary>
        public static bool AllowModelSwitching = true;

        /// <summary>Whether telephone information should be shown</summary>
        public static bool ShowTelephone = true;

        /// <summary>Whether Processes should be shown</summary>
        public static bool ShowProcesses = true;

        /// <summary>Whether views should be shown</summary>
        public static bool ShowViews = true;

        /// <summary>Whether units are shown (right-click menu or search)</summary>
        public static bool ShowUnits = true;

        /// <summary>Whether loads are shown (right-click menu or search)</summary>
        public static bool ShowLoads = true;

        /// <summary>Whether shunt compensators are shown (right-click menu or search)</summary>
        public static bool ShowShuntCompensators = true;

        /// <summary>Whether transformers are shown (right-click menu or search)</summary>
        public static bool ShowTransformers = true;

        /// <summary>Whether buses are shown (right-click menu or search)</summary>
        public static bool ShowBuses = true;

        /// <summary>Whether estimated MVAs are displayed (on network map, or right-click menu)</summary>
        public static bool ShowEstMVAs = true;

        /// <summary>Whether estimated voltages and pUs are displayed (on network map, or right-click menu)</summary>
        public static bool ShowEstVs = true;

        /// <summary>Whether telemetered voltages and pUs are displayed (on network map, or right-click menu)</summary>
        public static bool ShowTelVs = true;

        /// <summary>Whether telemetered MVAs are displayed (on network map, or right-click menu)</summary>
        public static bool ShowTelMVAs = true;

        /// <summary>Whether estimated MVARs are displayed (on network map, or right-click menu)</summary>
        public static bool ShowEstMVARs = true;

        /// <summary>Whether telemetered MVARs are displayed (on network map, or right-click menu)</summary>
        public static bool ShowTelMVARs = true;

        /// <summary>Whether estimated MWs are displayed (on network map, or right-click menu)</summary>
        public static bool ShowEstMWs = true;

        /// <summary>Whether telemetered MWs are displayed (on network map, or right-click menu)</summary>
        public static bool ShowTelMWs = true;

        /// <summary>Whether limits are displayed (on network map, or right-click menu)</summary>
        public static bool ShowLimits = true;

        /// <summary>Whether loading percentages (MVA/Normal limit) are displayed (on network map, or right-click menu)</summary>
        public static bool ShowPercentageLoading = true;

        /// <summary>Whether the user  call up one-lines</summary>
        public static bool ShowOneLines = true;

        /// <summary>Whether the user can move the map around elements</summary>
        public static bool ShowMoveMap = true;

        /// <summary>Whether the user  Show notes</summary>
        public static bool ShowNotes = true;

        /// <summary>Whether the user can call up the property page</summary>
        public static bool ShowPropertyPage = false;

        /// <summary>Whether the user  Show Events</summary>
        public static bool ShowEvents = true;

        /// <summary>Whether the user  Show the list of elements within a substation</summary>
        public static bool ShowElementTypes = true;

        /// <summary>Whether the user  Show the list of KV Levels within a substation</summary>
        public static bool ShowKVLevels = true;

        /// <summary>Whether the user can see a line's distance</summary>
        public static bool ShowDistance = true;

        /// <summary>Whether counties are shown on substation displays</summary>
        public static bool ShowCounties = true;

        /// <summary>Whether the website option is shown on substation displays</summary>
        public static bool ShowWebsite = false;

        /// <summary>Whether TEIDs are shown</summary>
        public static bool ShowTEIDs = true;

        /// <summary>Whether the user  create notes</summary>
        public static bool CreateNotes = true;

        /// <summary>Whether the search feature is available to the user</summary>
        public static bool SearchAvailable = true;

        /// <summary>Whether the user  draw a standard lasso</summary>
        public static bool LassoStandard = true;

        /// <summary>Whether the user  create an lasso including invisible elements</summary>
        public static bool LassoInvisible = true;

        /// <summary>Whether the user  Show EMS ICCP/ISD links</summary>
        public static bool DisplayICCPISDLinks = true;

        /// <summary>Whether the user  Show the communications status between MM and connected systems</summary>
        public static bool DisplayMMCommLinks = true;

        /// <summary>Whether the user  Show energized lines</summary>
        public static bool ShowEnergizedLines = true;

        /// <summary>Whether the user  Show de-energized lines</summary>
        public static bool ShowDeenergizedLines = true;

        /// <summary>Whether the user  Show partially energized lines</summary>
        public static bool ShowPartiallyEnergizedLines = true;

        /// <summary>How far back the user  go in history</summary>
        public static TimeSpan HistoricalTime = TimeSpan.FromSeconds(0);

        /// <summary>How far into the future the user  go</summary>
        public static TimeSpan FutureTime = TimeSpan.FromSeconds(0);

        /// <summary>Whether boilerplate text needs to be added</summary>
        public static string BoilerplateText = "";

        /// <summary>Whether the user's activity should be logged</summary>
        public static bool LogActivity = false;

        /// <summary>Whether the unit's generation type is shown</summary>
        public static bool ShowUnitGenerationType = true;

        /// <summary>Whether the default connectors should be used</summary>
        public static bool DefaultConnectors = false;

        /// <summary>Whether the user can create a savecase</summary>
        public static bool CreateSavecase = true;

        /// <summary>Whether the user can access the display properties page</summary>
        public static bool DisplayProperties = true;

        /// <summary>The maximum zoom level for the network map</summary>
        public static int MaxZoom = 30;

        /// <summary>The miniumum zoom level for the network map.</summary>
        public static int MinZoom = 5;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize the MM permissions
        /// </summary>
        /// <param name="xConfig">The configuration element for this permissions.</param>
        public static void InitializePermissions(XmlElement xConfig)
        {
            if (xConfig != null)
                foreach (XmlAttribute xAttr in xConfig.Attributes)
                    try
                    {
                        FieldInfo fI = typeof(MM_Permissions).GetField(xAttr.Name);
                        if (fI.FieldType == typeof(bool))
                            fI.SetValue(typeof(MM_Permissions), XmlConvert.ToBoolean(xAttr.Value));
                        else if (fI.FieldType == typeof(String))
                            fI.SetValue(typeof(MM_Permissions), xAttr.Value);
                        else if (fI.FieldType == typeof(int))
                            fI.SetValue(typeof(MM_Permissions), XmlConvert.ToInt32(xAttr.Value));
                    }
                    catch (Exception ex)
                    {
                        MM_Log.LogEvent(new MacomberMap.Common.Events.MM_Event_PermissionLoadingError(xAttr.Name, xAttr.Value, ex.Message));                        
                    }

            BoilerplateText = xConfig.InnerText;


        }
        #endregion

    }
}
